package com.stockz.watchlist.dao;

import java.util.List;

import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.stockz.equity.util.JpaUtil;
import com.stockz.watchlist.model.WatchList;

@Repository
public class WatchListDaoImpl implements WatchListDao {

	@Autowired
	private JpaUtil jpaUtil;
	
	@SuppressWarnings("unchecked")
	public List<String> getAllWatchListName() {
		return jpaUtil.getEntityManager().createQuery("select w.name from WatchList w").getResultList();
	}

	public WatchList get(Long id) {
		return jpaUtil.getEntityManager().find(WatchList.class, id);
	}

	public WatchList get(String name) {
		Query query = jpaUtil.getEntityManager().createQuery("select w from WatchList w where w.name = :name", WatchList.class);
		query.setParameter("name", name);
		return (WatchList)query.getSingleResult();
	}
	
	public void remove(WatchList watchList) {
		jpaUtil.getEntityManager().remove(watchList);
	}

	public void persist(WatchList watchList) {
		jpaUtil.getEntityManager().persist(watchList);
	}

	public WatchList merge(WatchList watchList) {
		return jpaUtil.getEntityManager().merge(watchList);
	}

	public String getWatchListNameById(Long id) {
		Query query = jpaUtil.getEntityManager().createQuery("select w.name from WatchList w where w.id = :id");
		query.setParameter("id", id);
		return (String)query.getSingleResult();
	}

}
